<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线考试平台</title>
    <link rel="shortcut icon" href="../images/exam.png" type="image/x-icon">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript"  src="../lib/layui/layui.js"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15" style="background: #f7f8fc">
        <form class="layui-form layui-form-pane" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">考试名</label>
                <div class="layui-input-block">
                    <input type="text" name="e_name" lay-verify="required" autocomplete="off"
                           placeholder="请输入内容"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属科目</label>
                <div class="layui-input-block">
                    <select lay-filter="test" name="s_id" class="subject">
                    </select>
                </div>
            </div>
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">试卷难度</label>
                <div class="layui-input-block">
                    <input type="radio" lay-filter="test" name="e_level" value="随机" title="随机" checked="">
                    <input type="radio" lay-filter="test" name="e_level" value="简单" title="简单">
                    <input type="radio" lay-filter="test" name="e_level" value="中等" title="中等">
                    <input type="radio" lay-filter="test" name="e_level" value="困难" title="困难">
                    <!--                        <select lay-filter="test" name="exam_paper_level">-->
                    <!--                            <option value="随机">随机</option>-->
                    <!--                            <option value="简单">简单</option>-->
                    <!--                            <option value="中等">中等</option>-->
                    <!--                            <option value="困难">困难</option>-->
                    <!--                        </select>-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">试卷名称</label>
                <div class="layui-input-block">
                    <select lay-filter="test1" name="ep_id" class="exam_paper">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">单选题数量</label>
                    <div class="layui-input-block">
                        <input type="text" name="choice_question_total" value="0" readonly="readonly" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">单选题分值</label>
                    <div class="layui-input-inline">
                        <input type="number" name="e_scq_score" oninput="total_score()" value="0" min="1" max="5" placeholder="1-5" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">判断题数量</label>
                    <div class="layui-input-inline">
                        <input type="text" name="judgment_question_total" value="0" readonly="readonly" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">判断题分值</label>
                    <div class="layui-input-inline">
                        <input type="number" name="e_jq_score" value="0" oninput="total_score()" min="1" max="5" placeholder="1-5" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">填空数量</label>
                    <div class="layui-input-block">
                        <input type="text" name="fill_blanks_question_total" value="0" readonly="readonly" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">填空题分值</label>
                    <div class="layui-input-inline">
                        <input type="number" name="e_fbq_score" oninput="total_score()" value="0" min="1" max="5" placeholder="1-5" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">简答题数量</label>
                    <div class="layui-input-inline">
                        <input type="text" name="short_answer_question_total" value="0" readonly="readonly" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">简答题分值</label>
                    <div class="layui-input-inline">
                        <input type="number" name="e_saq_score" value="0" oninput="total_score()" min="5" max="20" placeholder="5-20" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">总分</label>
                    <div class="layui-input-block">
                        <input type="text" name="e_total_score" readonly="readonly" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">及格分数线</label>
                    <div class="layui-input-inline">
                        <input type="number" name="e_pass_score" value="0" min="0" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">考试学生</label>
                <div class="layui-input-block">
                    <div id="exam_user" style="width: 300px"></div>
                </div>
            </div>
            <div class="layui-form-item">
                    <label class="layui-form-label">时间范围</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" name="time_frame" id="time_frame" placeholder="请选择开始-截止时间">
                    </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">考试时长</label>
                <div class="layui-input-inline">
                    <input type="number" name="e_duration" lay-verify="required" autocomplete="off"
                           placeholder="请输入内容"
                           class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"><span style="color: red">*</span>分钟</div>
            </div>


            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="release_exam">提交</button>
            </div>
        </form>
    </div>
</div>
</body>
<script type="text/javascript"  src="../lib/layui/layui.js"></script>
<script type="text/javascript" src="../js/xadmin.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    var exam_user;
    layui.config({
        base: '../xmSelect/'
    }).extend({
        xmSelect: 'xm-select'
    }).use(['element', "form", 'jquery', 'layer', 'xmSelect','laydate'], function () {
        const form = layui.form;
        const $ = layui.jquery;
        const layer = layui.layer;
        const element = layui.element;
        const xmSelect = layui.xmSelect;
        const laydate = layui.laydate;
        get_all_subject();
        get_exam_paper();
        get_class_user();

        form.on('select(test)', function (data) {
            get_exam_paper();
        });
        form.on('radio(test)', function (data) {
            get_exam_paper();
        });
        form.on('select(test1)', function (data) {
            get_exam_paper_by_ep_id();
        });

        laydate.render({
            elem: '#time_frame'
            ,type: 'datetime'
            ,range: '至'
        });

        form.on("submit(release_exam)", function (data) {
            console.log("aaaa");
            const e_name = data.field.e_name;
            const s_id = data.field.s_id;
            const e_level = data.field.e_level;
            const ep_id = data.field.ep_id;
            const e_scq_score = data.field.e_scq_score;
            const e_jq_score = data.field.e_jq_score;
            const e_saq_score = data.field.e_saq_score;
            const e_fbq_score = data.field.e_fbq_score;
            const e_total_score = data.field.e_total_score;
            const e_pass_score = data.field.e_pass_score;
            const e_duration = data.field.e_duration;
            const time_frame = data.field.time_frame.split('至');
            const e_begin_time = time_frame[0];
            const e_end_time = time_frame[1];
            const u_ids = exam_user.getValue('value').toString();

            $.ajax({
                url: 'add_exam',
                dataType: "json",//数据格式
                type: "post",//请求方式
                // contentType:'application/json;charset=UTF-8',
                async: false,//是否异步请求
                data: {
                    e_name: e_name,
                    s_id: s_id,
                    e_level: e_level,
                    ep_id: ep_id,
                    e_scq_score: e_scq_score,
                    e_jq_score: e_jq_score,
                    e_saq_score: e_saq_score,
                    e_fbq_score: e_fbq_score,
                    e_total_score: e_total_score,
                    e_pass_score: e_pass_score,
                    e_duration: e_duration,
                    e_begin_time: e_begin_time,
                    e_end_time: e_end_time ,
                    u_ids:u_ids
                },
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg(data.message, {icon: 5, time: 1000})
                    } else if (data.code === 200) {
                        layer.confirm(data.message, {icon: 6}, function () {
                            window.location.reload();
                        })
                    } else {
                        layer.msg('系统繁忙', {icon: 5, time: 1000})
                    }
                },
                error: function (e) {
                    alert("出现错误！！");
                }
            });
            return false

        });

    });

    function total_score(){
        layui.use(['element', "form", 'jquery', 'layer'], function () {
            const form = layui.form;
            const data = form.val("example");
            const cq_total =Number(data.choice_question_total);
            const e_scq_score =Number(data.e_scq_score);
            const jq_total =Number(data.judgment_question_total);
            const e_jq_score =Number(data.e_jq_score);
            const saq_total =Number(data.short_answer_question_total);
            const e_saq_score =Number(data.e_saq_score);
            const fbq_total =Number(data.fill_blanks_question_total);
            const e_fbq_score =Number(data.e_fbq_score);

            const e_total_score=(cq_total*e_scq_score)+(jq_total*e_jq_score)+(saq_total*e_saq_score)+(fbq_total*e_fbq_score);
            form.val("example", {
                "e_total_score": e_total_score
            });
            form.render();

        })
    }

    function get_all_subject() {
        layui.use(['element', "form", 'jquery', 'layer'], function () {
            const form = layui.form;
            $.ajax({
                url: 'get_all_subject',
                dataType: "json",//数据格式
                type: "post",//请求方式
                async: false,//是否异步请求
                success: function (data) {
                    if (data.code === 200) {
                        var text = ""
                        for (let i = 0; i < data.result.length; i++) {
                            text += "<option value=\"" + data.result[i].s_id + "\">" + data.result[i].s_name + "</option>"
                        }
                        $(".subject").html(text);
                        form.render();
                    }
                },
                error: function (e) {
                    alert("出现错误！！");
                }
            });
        })
    }

    function get_exam_paper_by_ep_id(){
        layui.use(['element', "form", 'jquery', 'layer'], function () {
            const form = layui.form;
            const $ = layui.jquery;
            const layer = layui.layer;
            const data = form.val("example");
            const ep_id = data.ep_id;
            $.ajax({
                url: 'get_exam_paper_by_ep_id',
                dataType: "json",//数据格式
                type: "post",//请求方式
                async: false,//是否异步请求
                data: {
                    ep_id: Number(ep_id)
                },
                success: function (data) {
                    if (data.code === 200) {
                        var num1=(data.result.scq_data.length)
                        var num2=(data.result.jq_data.length)
                        var num3=(data.result.saq_data.length)
                        var num4=(data.result.fbq_data.length)
                        form.val("example", {
                            "choice_question_total": num1
                            , "judgment_question_total": num2
                            , "short_answer_question_total": num3
                            , "fill_blanks_question_total": num4
                        });
                        form.render();
                    }
                },
                error: function (e) {
                    alert("出现错误！！");
                }
            });
        });
    }

    function get_exam_paper(){
        layui.use(['element', "form", 'jquery', 'layer'], function () {
            const form = layui.form;
            const $ = layui.jquery;
            const layer = layui.layer;
            const data = form.val("example");
            const s_id = data.s_id;
            const ep_level = data.e_level;

            $.ajax({
                url: 'get_exam_paper_by_s_id_and_level',
                dataType: "json",//数据格式
                type: "post",//请求方式
                async: false,//是否异步请求
                data: {
                    s_id: s_id,
                    ep_level: ep_level
                },
                success: function (data) {
                    if (data.code === 200) {
                        var text = "<option value=\"0\">请选择</option>"
                        for (let i = 0; i < data.result.length; i++) {
                            text += "<option value=\"" + data.result[i].ep_id + "\">" + data.result[i].ep_name + "</option>"
                        }
                        $(".exam_paper").html(text);
                        form.render();
                    }
                },
                error: function (e) {
                    alert("出现错误！！");
                }
            });
        });
    }

    function getDateDiff(dateTimeStamp) {
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - dateTimeStamp;
        if (diffValue < 0) {
            //若日期不符则弹出窗口告之
            //alert("结束日期不能小于开始日期！");
        }
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;
        if (monthC >= 1) {
            result = "创建于" + parseInt(monthC) + "个月前";
        } else if (weekC >= 1) {
            result = "创建于" + parseInt(weekC) + "周前";
        } else if (dayC >= 1) {
            result = "创建于" + parseInt(dayC) + "天前";
        } else if (hourC >= 1) {
            result = "创建于" + parseInt(hourC) + "个小时前";
        } else if (minC >= 1) {
            result = "创建于" + parseInt(minC) + "分钟前";
        } else
            result = "刚刚创建";
        return result;
    }

    //js函数代码：字符串转换为时间戳
    function getDateTimeStamp(dateStr) {
        var time_stamp = Date.parse(dateStr.replace(/-/gi, "/"));
        return getDateDiff(time_stamp)
    }

    function get_class_user(){
        layui.use(['element', "form", 'jquery', 'layer'], function () {
            const form = layui.form;

            exam_user = xmSelect.render({
                el: '#exam_user',
                autoRow: true,
                filterable: true,
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [ -3 ],
                    strict: true
                },
                model: {
                    label: {
                        block: {
                            template: function (item, sels) {
                                return item.value;
                            },
                        },
                    }
                },
                template({item, sels, name, value}) {
                    return item.name + '<span style="position: absolute; right: 10px; color: #8799a3">' + value + '</span>'
                },
                toolbar: {
                    show: true,
                    list: ['ALL', 'REVERSE', 'CLEAR']
                },
                height: 'auto',
                data: []
            })

            $.ajax({
                url: 'get_class_user',
                dataType: "json",//数据格式
                type: "post",//请求方式
                async: false,//是否异步请求
                success: function (data) {
                    if (data.code === 200) {
                        var class_user_data=[];
                        for (let i = 0; i < data.result.length; i++) {
                            var c_name=data.result[i].c_name;
                            var c_id=data.result[i].c_id;
                            var user_data=[];
                            for (let j = 0; j < data.result[i].eClassUsers.length; j++) {
                                var u_id=data.result[i].eClassUsers[j].u_id;
                                var u_sno=data.result[i].eClassUsers[j].sysUser.u_sno;
                                user_data.push({name:u_sno,value:u_id})
                            }
                            class_user_data.push({name:c_name,value:c_id,children:user_data})
                        }
                        exam_user.update({
                            data: class_user_data
                        })
                        form.render();
                    }
                },
                error: function (e) {
                    alert("出现错误！！");
                }
            });
        })
    }


</script>

</html>